package fr.castorhoz.isi_connectivity_widget.wifi_widget;

import android.content.Context;
import android.database.Cursor;
import android.net.wifi.WifiManager;
import android.util.Log;
import fr.castorhoz.isi_connectivity_widget.ConnButton;
import fr.castorhoz.isi_connectivity_widget.R;
import fr.castorhoz.isi_connectivity_widget.database.DBAdapter;
import fr.castorhoz.isi_connectivity_widget.database.DataBaseHelper;


public class WifiButton extends ConnButton {

	public final static String WIFI_DISABLED = "wifiDisabled";
	public final static String WIFI_ENABLED = "wifiEnabled";

	private static DBAdapter dbAdapter;

	private Context context;

	public WifiButton(Context context, int widgetId) {
		
		this.context = context;
		this.appWidgetId = widgetId;
	
		WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		wm.getWifiState();
		detectWifiConnectivityState(wm.getWifiState());
	}

	private void detectWifiConnectivityState(int info) {
		Log.i(this.getClass().getName(), "detecting wifi state connectivity");
		dbAdapter = new DBAdapter(context);		
		
		switch (info) {
		case WifiManager.WIFI_STATE_ENABLED:
			Log.i(this.getClass().getName(), "registering wifi state : WIFI_STATE_ENABLED");
			changeWifiDBValue(WifiButton.WIFI_ENABLED);
			break;
			
		case WifiManager.WIFI_STATE_DISABLED:
			Log.i(this.getClass().getName(), "registering wifi state : WIFI_STATE_DISABLED");
			changeWifiDBValue(WifiButton.WIFI_DISABLED);
			break;
			
		default:
			break;
		}
	}
	
	private void changeWifiDBValue(String value) {
		dbAdapter.open();
		dbAdapter.removeWifiWidgetStates();
		dbAdapter.insertWifiWidgetState(value);
		dbAdapter.close();
		Log.d(this.getClass().getName(), "wifi state is saved into database");
	}
	
	/**
	 * Generic button. Do not use this to add a button to into a widget
	 */
	public WifiButton() {}
	
	public static void doEnableNetwork(Context context) {
		WifiManager mgr = getWifiManager(context);
		mgr.setWifiEnabled(true);
	}
	
	public static void doDisableNetwork(Context context) {
		WifiManager mgr = getWifiManager(context);
		mgr.setWifiEnabled(false);
	}

	@Override
	public void enableNetwork(Context context) {
		doEnableNetwork(context);
	}

	@Override
	public void disableNetwork(Context context) {
		doDisableNetwork(context);
	}

	private static WifiManager getWifiManager(Context context) {
		return (WifiManager) context.getApplicationContext().getSystemService(
				Context.WIFI_SERVICE);
	}
	
	public static String doGetAction() {
		return "fr_imberle_connectivityWidget_CLICK_WIFI";
	}
	
	@Override
	public String getAction(){
		return doGetAction();
	}
	
	@Override
	public int getSelectorOff() {
		return R.drawable.wifi_selector_off;
	}
	
	@Override
	public int getSelectorOn() {
		return R.drawable.wifi_selector_on;
	}
	
	@Override
	public int getSelectorUnknown() {
		return R.drawable.wifi_selector_unknown;
	}

	@Override
	public int getCurrentSelector() {
		return getWifiStateFromDatabse(context) ? getSelectorOn() : getSelectorOff();
	}
	
	/**
	 * Gets the state of wifi from database
	 * @param context
	 * @return
	 */
	public static boolean getWifiStateFromDatabse(Context context) {
		dbAdapter = new DBAdapter(context);
		String wifiState = null;
		dbAdapter.open();
		Cursor c = dbAdapter.getWifiWidgetState();
		if (c.moveToNext()) {
			wifiState = c
					.getString(c
							.getColumnIndexOrThrow(DataBaseHelper.WIFI_STATE_COLUMNNAME));
		}
		c.close();
		dbAdapter.close();
		// TODO deal with nothing registered in database (must look if
		// ConnectivityManager get a type_mobile CONNECTED)
		return wifiState != null && wifiState.equals(WIFI_ENABLED);
	}

	@Override
	public boolean getState(Context context) {
		return getWifiStateFromDatabse(context);
	}


}
